/**
 * 
 */
package com.tech.freak.pyp;

import android.app.Activity;
import android.content.ContentUris;
import android.content.ContentValues;
import android.content.Intent;
import android.database.Cursor;
import android.database.DatabaseUtils;
import android.net.Uri;
import android.os.Bundle;
import android.support.v4.content.CursorLoader;
import android.support.v4.content.Loader;
import android.support.v4.content.Loader.OnLoadCompleteListener;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import com.tech.freak.pyp.data.PicoyPlaca.PYP;

/**
 * @author julian
 *
 */
public class EditCity extends Activity {

	public static final String DATA_ID = "_id";

	private static final String TAG = "EditCity";
	
	private Long dataId;
	
	private CursorLoader loader;
	
	OnLoadCompleteListener<Cursor> loaderListener;
	
	TextView city;
	EditText monday;
	EditText tuesday;
	EditText wednesday;
	EditText thursday;
	EditText friday;
	EditText saturday;
	EditText sunday;
	
	Button save_btn;
	Button cancel_btn;
	/* (non-Javadoc)
	 * @see android.app.Activity#onCreate(android.os.Bundle)
	 */
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.edit_city);
		
		Bundle extras = getIntent().getExtras();
		if(extras!=null){
			dataId = extras.getLong(DATA_ID);
		}
		
		if(dataId==null && savedInstanceState!=null){
			dataId = savedInstanceState.getLong(DATA_ID);
		}
		
		//Get EditTexts
		getEditTexts();
		
		save_btn = (Button) findViewById(R.id.save_button);
		save_btn.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View arg0) {
				saveData();
				finish();
				
			}
		});
		
		cancel_btn = (Button) findViewById(R.id.cancel_button);
		cancel_btn.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				finish();
				
			}
		});
		
		prepareDataLoader();
	}


	/* (non-Javadoc)
	 * @see android.app.Activity#onResume()
	 */
	@Override
	protected void onResume() {
		super.onResume();
		
		loadData();
	}

	/* (non-Javadoc)
	 * @see android.app.Activity#onPause()
	 */
	@Override
	protected void onPause() {
		if(loader.isStarted()){
			loader.unregisterListener(loaderListener);
			loader.stopLoading();
		}
		
		super.onPause();
	}

	/* (non-Javadoc)
	 * @see android.app.Activity#onSaveInstanceState(android.os.Bundle)
	 */
	@Override
	protected void onSaveInstanceState(Bundle outState) {
		super.onSaveInstanceState(outState);
		outState.putLong(DATA_ID, dataId);
	}
	

	private void getEditTexts() {
		city = (TextView) findViewById(R.id.city);
		monday = (EditText) findViewById(R.id.monday_content);
		tuesday = (EditText) findViewById(R.id.tuesday_content);
		wednesday = (EditText) findViewById(R.id.wednesday_content);
		thursday = (EditText) findViewById(R.id.thursday_content);
		friday = (EditText) findViewById(R.id.friday_content);
		saturday = (EditText) findViewById(R.id.saturday_content);
		sunday = (EditText) findViewById(R.id.sunday_content);
		
	}
	
	private void loadData(){
		
		loaderListener = new OnLoadCompleteListener<Cursor>() {

			@Override
			public void onLoadComplete(Loader<Cursor> loader, Cursor data) {
				bindData(data);
			}
		};
		
		loader.registerListener(0, loaderListener);
		loader.startLoading();
	}
	
	protected void bindData(Cursor cursor) {
		Log.d(TAG,DatabaseUtils.dumpCursorToString(cursor));
		if(cursor.moveToFirst()){
		
			city.setText(cursor.getString(cursor.getColumnIndex(PYP.CITY)));
			monday.setText(cursor.getString(cursor.getColumnIndex(PYP.MONDAY)));
			tuesday.setText(cursor.getString(cursor.getColumnIndex(PYP.TUESDAY)));
			wednesday.setText(cursor.getString(cursor.getColumnIndex(PYP.WEDNSEDAY)));
			thursday.setText(cursor.getString(cursor.getColumnIndex(PYP.THURSDAY)));
			friday.setText(cursor.getString(cursor.getColumnIndex(PYP.FRIDAY)));
			saturday.setText(cursor.getString(cursor.getColumnIndex(PYP.SATURDAY)));
			sunday.setText(cursor.getString(cursor.getColumnIndex(PYP.SUNDAY)));
		}
	}

	private void prepareDataLoader() {
		String[] projection = new String[] {PYP._ID , 
				PYP.CITY,
				PYP.MONDAY,
				PYP.TUESDAY,
				PYP.WEDNSEDAY,
				PYP.THURSDAY,
				PYP.FRIDAY,
				PYP.SATURDAY,
				PYP.SUNDAY
				};
		Uri uri = ContentUris.withAppendedId(PYP.CONTENT_URI, dataId);
	
		loader = new CursorLoader(this, uri, projection, null,null, null);
		
	}
	
	protected void saveData() {
		ContentValues cv = fillContentValues();
		
		if(cv== null){
			Toast.makeText(this, R.string.notification_update_city_error, Toast.LENGTH_LONG).show();
		}else{
			Utils.updateCityInfo(getContentResolver(), ContentUris.withAppendedId(PYP.CONTENT_URI, dataId), cv);
			
			//Update widgets
			sendBroadcast(new Intent().setAction(Utils.UPDATE_WIDGET));
		}
	}


	private ContentValues fillContentValues() {
		ContentValues result = new ContentValues();
		
		String pypInfo = monday.getText().toString();
		if(!validInput(pypInfo)){
			return null;
		}
		else{
			result.put(PYP.MONDAY, pypInfo);
		}
		
		pypInfo = tuesday.getText().toString();
		if(!validInput(pypInfo)){
			return null;
		}
		else{
			result.put(PYP.TUESDAY, pypInfo);
		}
		
		pypInfo = wednesday.getText().toString();
		if(!validInput(pypInfo)){
			return null;
		}
		else{
			result.put(PYP.WEDNSEDAY, pypInfo);
		}
		
		pypInfo = thursday.getText().toString();
		if(!validInput(pypInfo)){
			return null;
		}
		else{
			result.put(PYP.THURSDAY, pypInfo);
		}
		
		pypInfo = friday.getText().toString();
		if(!validInput(pypInfo)){
			return null;
		}
		else{
			result.put(PYP.FRIDAY, pypInfo);
		}

		pypInfo = saturday.getText().toString();
		if(!validInput(pypInfo)){
			return null;
		}
		else{
			result.put(PYP.SATURDAY, pypInfo);
		}
		
		pypInfo = sunday.getText().toString();
		if(!validInput(pypInfo)){
			return null;
		}
		else{
			result.put(PYP.SUNDAY, pypInfo);
		}
		
		return result;
	}


	private boolean validInput(String pypInfo) {
		boolean result = false;
		
		if(TextUtils.isEmpty(pypInfo)){
			result = true;
		}else if(pypInfo.matches("([0-9],)*[0-9]")){
			result = true;
		}
		
		return result;
	}
}
